package com.spark.incoicings.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spark.incoicings.dao.OrderDetailMapper;
import com.spark.incoicings.dao.ProductMapper;
import com.spark.incoicings.dao.SalesOrderMapper;
import com.spark.incoicings.service.ShipmentDetailService;
import com.spark.invoicings.entity.OrderDetail;
import com.spark.invoicings.entity.Product;
import com.spark.invoicings.entity.SalesOrder;
import com.spark.invoicings.entity.ShipmentDetail;
import com.spark.invoicings.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/login")
@Slf4j
public class ShipmentDetailController {
    @Resource
    private ShipmentDetailService shipmentDetailService;
    @Resource
    private OrderDetailMapper orderDetailMapper;
    @Resource
    private SalesOrderMapper salesOrderMapper;
    @Resource
    private ProductMapper productMapper;
    @GetMapping("/selectShipmentDeliveryAll")
    public Result selectShipmentDeliveryAll(@RequestParam(defaultValue = "1")Integer pageNum,
                                            @RequestParam(defaultValue = "3")Integer pageSize,
                                            @RequestParam(required = false)Integer id){
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",id);
        PageHelper.startPage(pageNum,pageSize);
        List<ShipmentDetail> shipmentDetails = shipmentDetailService.selectShipmentDeliveryAll(map);
        shipmentDetails.forEach(shipmentDetail -> {
            OrderDetail orderDetail = orderDetailMapper.selectById(shipmentDetail.getOrderDetailId());
            SalesOrder salesOrder = salesOrderMapper.selectById(orderDetail.getSalesOrderId());
            orderDetail.setSalesOrder(salesOrder);
            shipmentDetail.setOrderDetail(orderDetail);
        });
        PageInfo<ShipmentDetail> pageInfo = new PageInfo<>(shipmentDetails);
        return Result.success(200,"success",pageInfo);
    }
    @GetMapping("/selectShipmentDeliveryProduct")
    public Result selectShipmentDeliveryProduct(Integer id){
        SalesOrder salesOrder = salesOrderMapper.selectById(id);
        List<OrderDetail> orderDetails = orderDetailMapper.selectList(new QueryWrapper<OrderDetail>().eq("sales_order_id", salesOrder.getOrderId()));
        orderDetails.forEach(orderDetail -> {
            Product product = productMapper.selectById(orderDetail.getProductId());
            orderDetail.setProducts(product);
        });
        salesOrder.setOrderDetail(orderDetails);
        return Result.success(200,"success",salesOrder);
    }
    @GetMapping("/selectDetailsProduct")
    public Result selectDetailsProduct(Integer id){
        Product product = productMapper.selectById(id);
        return Result.success(200,"success",product);
    }

    /**
     * 添加发货明细
     * @param shipmentDetail
     * @return
     */
    @PostMapping("/saveShipmentDelivery")
    public Result saveShipmentDelivery(@RequestBody ShipmentDetail shipmentDetail){
        shipmentDetailService.save(shipmentDetail);
        return Result.success();
    }

    /**
     * 修改发货明细
     * @param shipmentDetail
     * @return
     */
    @PostMapping("/updateShipmentDelivery")
    public Result updateShipmentDelivery(@RequestBody ShipmentDetail shipmentDetail){
        shipmentDetailService.updateById(shipmentDetail);
        return Result.success();
    }
}
